//
//  RichView.swift
//  zhende
//
//  Created by 陆文宇 on 2017/6/11.
//  Copyright © 2017年 chenliang. All rights reserved.
//

import UIKit
import SnapKit

class YYRichView: UIView {
    
    var jsonArr: [[String: Any]]!
    var views = [UIView]()
    var changeHeight: ((CGFloat) -> Void)?
    
    init(frame: CGRect, jsonStr: String) {
        super.init(frame: CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.width, height: 0))
        
        handleJson(jsonStr)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func handleJson(_ jsonStr: String) {
        do {
            jsonArr = try JSONSerialization.jsonObject(with: jsonStr.data(using: .utf8)!, options: []) as! [[String: Any]]
            for json in jsonArr {
                let type = json["type"] as! Int
                let value = json["value"] as! String
                var view: UIView!
                if type == 2 {
                    let imageView = UIImageView()
                    view = imageView
                    addSubview(view)
                    view.snp.makeConstraints({ [unowned self] (maker) in
                        maker.left.equalToSuperview()
                        maker.right.equalToSuperview()
                        if let bottomView = self.views.last {
                            maker.top.equalTo(bottomView.snp.bottom).offset(8)
                        } else {
                            maker.top.equalTo(self.snp.top)
                        }
                        maker.height.equalTo(screenWidth / 6 * 5)
                    })
                    view!.yy_height = screenWidth / 6 * 5
                    imageView.kf.setImage(with: URL(string: value), placeholder: #imageLiteral(resourceName: "placeImage6-5"), completionHandler: { (image, error, cacheType, _) in
                        if error == nil {
                            if image == nil {
                                return
                            }
                            
                            view!.snp.updateConstraints({ (maker) in
                                maker.height.equalTo(screenWidth / image!.size.width * image!.size.height)
                            })
                            view!.yy_height = screenWidth / image!.size.width * image!.size.height
                            
                            if cacheType != .memory {
                                var totalHeight: CGFloat = 0
                                for view in self.views {
                                    totalHeight += view.yy_height + 8
                                }
                                self.changeHeight?(totalHeight)
                            }
                        }
                    })
                } else if type == 1 {
                    view = UILabel(frame: CGRect(x: 0, y: 0, width: frame.width - 30, height: CGFloat.infinity), aFont: mainFont, aString: value)
                    addSubview(view)
                    view.snp.makeConstraints({ [unowned self] (maker) in
                        maker.left.equalTo(15)
                        maker.right.equalTo(-15)
                        maker.top.equalTo(self.views.last?.snp.bottom ?? self.snp.top).offset(8)
                        maker.height.equalTo(view!.yy_height)
                    })
                }
                views.append(view)
            }
            
            var totalHeight: CGFloat = 0
            for view in views {
                totalHeight += view.yy_height + 8
            }
            yy_height = totalHeight
        } catch let error {
            yy_Log(error)
        }
    }

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
